<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    

    

    



    <meta charset="utf-8">
    
    
    <meta name="sogou_site_verification" content="true">
    
    
    
    <title>JavaScript常用代码集合 | Lvshen&#39;s Blog | This is My World</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="JavaScript,前端">
    <meta name="baidu-site-verification" content="VIVNdSiMZm">
    <meta name="description" content="手机类型判断  1234567var BrowserInfo = &amp;#123;        userAgent: navigator.userAgent.toLowerCase()    isAndroid: Boolean(navigator.userAgent.match(/android/ig)),    isIphone: Boolean(navigator.userAgent.mat">
<meta name="keywords" content="JavaScript,前端">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript常用代码集合">
<meta property="og:url" content="https://lvshen9.gitee.io/2017/08/29/JavaScript常用代码集合/index.html">
<meta property="og:site_name" content="Lvshen&#39;s Blog">
<meta property="og:description" content="手机类型判断  1234567var BrowserInfo = &amp;#123;        userAgent: navigator.userAgent.toLowerCase()    isAndroid: Boolean(navigator.userAgent.match(/android/ig)),    isIphone: Boolean(navigator.userAgent.mat">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2017-08-29T07:51:17.819Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaScript常用代码集合">
<meta name="twitter:description" content="手机类型判断  1234567var BrowserInfo = &amp;#123;        userAgent: navigator.userAgent.toLowerCase()    isAndroid: Boolean(navigator.userAgent.match(/android/ig)),    isIphone: Boolean(navigator.userAgent.mat">
    
    <link rel="shortcut icon" href="/img/mylogo.jpg">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">我的技术小房间</h5>
          <a href="mailto:https://lvshen9.github.io" title="https://lvshen9.github.io" class="mail">https://lvshen9.github.io</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/about"  >
                <i class="icon icon-lg icon-address-book"></i>
                About
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/collection"  >
                <i class="icon icon-lg icon-apple"></i>
                Collection
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://lvshen9.github.io/" target="_blank" >
                <i class="icon icon-lg icon-wordpress"></i>
                Blog
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/lvshen9" target="_blank" >
                <i class="icon icon-lg icon-github-alt"></i>
                GitHub
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">JavaScript常用代码集合</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">JavaScript常用代码集合</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-08-29T07:37:05.000Z" itemprop="datePublished" class="page-time">
  2017-08-29
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/技术/">技术</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    

<article id="post-JavaScript常用代码集合"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">JavaScript常用代码集合</h1>
        <div class="post-meta">
            <time class="post-time" title="2017-08-29 15:37:05" datetime="2017-08-29T07:37:05.000Z"  itemprop="datePublished">2017-08-29</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/技术/">技术</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <blockquote>
<p>手机类型判断</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> BrowserInfo = &#123;    </span><br><span class="line">    userAgent: navigator.userAgent.toLowerCase()</span><br><span class="line">    isAndroid: <span class="built_in">Boolean</span>(navigator.userAgent.match(<span class="regexp">/android/ig</span>)),</span><br><span class="line">    isIphone: <span class="built_in">Boolean</span>(navigator.userAgent.match(<span class="regexp">/iphone|ipod/ig</span>)),</span><br><span class="line">    isIpad: <span class="built_in">Boolean</span>(navigator.userAgent.match(<span class="regexp">/ipad/ig</span>)),</span><br><span class="line">    isWeixin: <span class="built_in">Boolean</span>(navigator.userAgent.match(<span class="regexp">/MicroMessenger/ig</span>)),</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<blockquote>
<p>返回字符串长度，汉字计数为2</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">strLength</span>(<span class="params">str</span>) </span>&#123; </span><br><span class="line">    <span class="keyword">var</span> a = <span class="number">0</span>;            </span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; str.length; i++) &#123;</span><br><span class="line">    <span class="keyword">if</span> (str.charCodeAt(i) &gt; <span class="number">255</span>)</span><br><span class="line">    a += <span class="number">2</span>;<span class="comment">//按照预期计数增加2</span></span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    a++;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> a;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>获取url中的参数</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">GetQueryStringRegExp</span>(<span class="params">name,url</span>) </span>&#123;</span><br><span class="line"><span class="keyword">var</span> reg = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"(^|\\?|&amp;)"</span> + name + <span class="string">"=([^&amp;]*)(\\s|&amp;|$)"</span>, <span class="string">"i"</span>); </span><br><span class="line"><span class="keyword">if</span> (reg.test(url)) </span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">decodeURIComponent</span>(<span class="built_in">RegExp</span>.$<span class="number">2.</span>replace(<span class="regexp">/\+/g</span>, <span class="string">" "</span>)); </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p><a href="http://lib.csdn.net/base/javascript" target="_blank" rel="noopener">js</a> 绑定事件 适用于任何浏览器的元素绑定</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">eventBind</span>(<span class="params">obj, eventType, callBack</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (obj.addEventListener) &#123;</span><br><span class="line">        obj.addEventListener(eventType, callBack, <span class="literal">false</span>);</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">window</span>.attachEvent) &#123;</span><br><span class="line">        obj.attachEvent(<span class="string">'on'</span> + eventType, callBack);</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        obj[<span class="string">'on'</span> + eventType] = callBack;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">eventBind(<span class="built_in">document</span>, <span class="string">'click'</span>, bodyClick);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>获得当前浏览器JS的版本</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getjsversion</span>(<span class="params"></span>)</span>&#123;    </span><br><span class="line">    <span class="keyword">var</span> n = navigator;    </span><br><span class="line">    <span class="keyword">var</span> u = n.userAgent;    </span><br><span class="line">    <span class="keyword">var</span> apn = n.appName;   </span><br><span class="line">    <span class="keyword">var</span> v = n.appVersion;    </span><br><span class="line">    <span class="keyword">var</span> ie = v.indexOf(<span class="string">'MSIE '</span>);    </span><br><span class="line">    <span class="keyword">if</span> (ie &gt; <span class="number">0</span>)&#123;</span><br><span class="line">        apv = <span class="built_in">parseInt</span>(i = v.substring(ie + <span class="number">5</span>));        </span><br><span class="line">        <span class="keyword">if</span> (apv &gt; <span class="number">3</span>) &#123;</span><br><span class="line">            apv = <span class="built_in">parseFloat</span>(i);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        apv = <span class="built_in">parseFloat</span>(v);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> isie = (apn == <span class="string">'Microsoft Internet Explorer'</span>);    </span><br><span class="line">    <span class="keyword">var</span> ismac = (u.indexOf(<span class="string">'Mac'</span>) &gt;= <span class="number">0</span>);    </span><br><span class="line">    <span class="keyword">var</span> javascriptVersion = <span class="string">"1.0"</span>;    </span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">String</span> &amp;&amp; <span class="built_in">String</span>.prototype) &#123;</span><br><span class="line">        javascriptVersion = <span class="string">'1.1'</span>;        </span><br><span class="line">        <span class="keyword">if</span> (javascriptVersion.match) &#123;</span><br><span class="line">            javascriptVersion = <span class="string">'1.2'</span>;            </span><br><span class="line">            <span class="keyword">var</span> tm = <span class="keyword">new</span> <span class="built_in">Date</span>;            </span><br><span class="line">            <span class="keyword">if</span> (tm.setUTCDate) &#123;</span><br><span class="line">                javascriptVersion = <span class="string">'1.3'</span>;                </span><br><span class="line">                <span class="keyword">if</span> (isie &amp;&amp; ismac &amp;&amp; apv &gt;= <span class="number">5</span>) </span><br><span class="line">                javascriptVersion = <span class="string">'1.4'</span>;                </span><br><span class="line">                <span class="keyword">var</span> pn = <span class="number">0</span>;                </span><br><span class="line">                <span class="keyword">if</span> (pn.toPrecision) &#123;</span><br><span class="line">                    javascriptVersion = <span class="string">'1.5'</span>;</span><br><span class="line">                    a = <span class="keyword">new</span> <span class="built_in">Array</span>;                    </span><br><span class="line">                    <span class="keyword">if</span> (a.forEach) &#123;</span><br><span class="line">                        javascriptVersion = <span class="string">'1.6'</span>;</span><br><span class="line">                        i = <span class="number">0</span>;</span><br><span class="line">                        o = <span class="keyword">new</span> <span class="built_in">Object</span>;</span><br><span class="line">                        tcf = <span class="keyword">new</span> <span class="built_in">Function</span>(<span class="string">'o'</span>, <span class="string">'var e,i=0;try&#123;i=new Iterator(o)&#125;catch(e)&#123;&#125;return i'</span>);</span><br><span class="line">                        i = tcf(o);                        </span><br><span class="line">                        <span class="keyword">if</span> (i &amp;&amp; i.next) &#123;</span><br><span class="line">                            javascriptVersion = <span class="string">'1.7'</span>;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;    </span><br><span class="line">    <span class="keyword">return</span> javascriptVersion;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>获取当前点击事件的Object对象</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getEvent</span>(<span class="params"></span>) </span>&#123;    </span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">document</span>.all) &#123;        </span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">window</span>.event; <span class="comment">//如果是ie</span></span><br><span class="line">    &#125;</span><br><span class="line">    func = getEvent.caller;    </span><br><span class="line">    <span class="keyword">while</span> (func != <span class="literal">null</span>) &#123;        </span><br><span class="line">    <span class="keyword">var</span> arg0 = func.arguments[<span class="number">0</span>];        </span><br><span class="line">    <span class="keyword">if</span> (arg0) &#123;            </span><br><span class="line">    <span class="keyword">if</span> ((arg0.constructor == Event || arg0.constructor == MouseEvent)</span><br><span class="line">|| (<span class="keyword">typeof</span> (arg0) == <span class="string">"object"</span> &amp;&amp; arg0.preventDefault &amp;&amp; arg0.stopPropagation)) &#123;</span><br><span class="line">            <span class="keyword">return</span> arg0;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        func = func.caller;</span><br><span class="line">    &#125;    </span><br><span class="line">    <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>字符串截取方法</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">getCharactersLen: <span class="function"><span class="keyword">function</span> (<span class="params">charStr, cutCount</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (charStr == <span class="literal">null</span> || charStr == <span class="string">''</span>) </span><br><span class="line">    <span class="keyword">return</span> <span class="string">''</span>;        </span><br><span class="line">    <span class="keyword">var</span> totalCount = <span class="number">0</span>;        </span><br><span class="line">    <span class="keyword">var</span> newStr = <span class="string">''</span>;        </span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; charStr.length; i++) &#123;            </span><br><span class="line">    <span class="keyword">var</span> c = charStr.charCodeAt(i);            </span><br><span class="line">    <span class="keyword">if</span> (c &lt; <span class="number">255</span> &amp;&amp; c &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        totalCount++;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        totalCount += <span class="number">2</span>;</span><br><span class="line">    &#125; </span><br><span class="line">    <span class="keyword">if</span> (totalCount &gt;= cutCount) &#123;</span><br><span class="line">        newStr += charStr.charAt(i);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        newStr += charStr.charAt(i);</span><br><span class="line">        &#125;</span><br><span class="line">   &#125;        </span><br><span class="line">   <span class="keyword">return</span> newStr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>JS 弹出新窗口全屏</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> tmp = <span class="built_in">window</span>.open(<span class="string">"about:blank"</span>, <span class="string">""</span>, <span class="string">"fullscreen=1"</span>)</span><br><span class="line">tmp.moveTo(<span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">tmp.resizeTo(screen.width + <span class="number">20</span>, screen.height);</span><br><span class="line">tmp.focus();</span><br><span class="line">tmp.location.href = <span class="string">'http://www.che168.com/pinggu/eva_'</span> + msgResult.message[<span class="number">0</span>] + <span class="string">'.html'</span>;</span><br><span class="line"><span class="keyword">var</span> config_ = <span class="string">"left=0,top=0,width="</span> + (<span class="built_in">window</span>.screen.Width) + <span class="string">",height="</span> + (<span class="built_in">window</span>.screen.Height);                            </span><br><span class="line"><span class="built_in">window</span>.open(<span class="string">'http://www.che168.com/pinggu/eva_'</span> + msgResult.message[<span class="number">0</span>] + <span class="string">'.html'</span>, <span class="string">"winHanle"</span>, config_);</span><br><span class="line"><span class="comment">//模拟form提交打开新页面</span></span><br><span class="line"><span class="keyword">var</span> f = <span class="built_in">document</span>.createElement(<span class="string">"form"</span>);</span><br><span class="line">f.setAttribute(<span class="string">'action'</span>, <span class="string">'http://www.che168.com/pinggu/eva_'</span> + msgResult.message[<span class="number">0</span>] + <span class="string">'.html'</span>);</span><br><span class="line">f.target = <span class="string">'_blank'</span>;</span><br><span class="line"><span class="built_in">document</span>.body.appendChild(f);</span><br><span class="line">f.submit();</span><br></pre></td></tr></table></figure>
<blockquote>
<p>全选/全不选</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">selectAll</span>(<span class="params">objSelect</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (objSelect.checked == <span class="literal">true</span>) &#123;</span><br><span class="line">    $(<span class="string">"input[name='chkId']"</span>).attr(<span class="string">"checked"</span>, <span class="literal">true</span>);</span><br><span class="line">    $(<span class="string">"input[name='chkAll']"</span>).attr(<span class="string">"checked"</span>, <span class="literal">true</span>);</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span> (objSelect.checked == <span class="literal">false</span>) &#123;</span><br><span class="line">    $(<span class="string">"input[name='chkId']"</span>).attr(<span class="string">"checked"</span>, <span class="literal">false</span>);</span><br><span class="line">    $(<span class="string">"input[name='chkAll']"</span>).attr(<span class="string">"checked"</span>, <span class="literal">false</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>js 判断浏览器</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">判断是否是 IE 浏览器</span><br><span class="line"><span class="keyword">if</span> (<span class="built_in">document</span>.all)&#123; </span><br><span class="line">    alert(”IE浏览器”); </span><br><span class="line">&#125;<span class="keyword">else</span>&#123; </span><br><span class="line">    alert(”非IE浏览器”); </span><br><span class="line">&#125; </span><br><span class="line"><span class="keyword">if</span> (!!<span class="built_in">window</span>.ActiveXObject)&#123; </span><br><span class="line">    alert(”IE浏览器”); </span><br><span class="line">&#125;<span class="keyword">else</span>&#123; </span><br><span class="line">    alert(”非IE浏览器”); </span><br><span class="line">&#125; </span><br><span class="line">判断是IE几</span><br><span class="line"><span class="keyword">var</span> isIE=!!<span class="built_in">window</span>.ActiveXObject; </span><br><span class="line"><span class="keyword">var</span> isIE6=isIE&amp;&amp;!<span class="built_in">window</span>.XMLHttpRequest; </span><br><span class="line"><span class="keyword">var</span> isIE8=isIE&amp;&amp;!!<span class="built_in">document</span>.documentMode; </span><br><span class="line"><span class="keyword">var</span> isIE7=isIE&amp;&amp;!isIE6&amp;&amp;!isIE8; </span><br><span class="line"><span class="keyword">if</span> (isIE)&#123; </span><br><span class="line">    <span class="keyword">if</span> (isIE6)&#123; </span><br><span class="line">        alert(”ie6″); </span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span> (isIE8)&#123; </span><br><span class="line">        alert(”ie8″); </span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span> (isIE7)&#123; </span><br><span class="line">        alert(”ie7″); </span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>判断浏览器</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getOs</span>(<span class="params"></span>) </span>&#123;    </span><br><span class="line"><span class="keyword">if</span> (navigator.userAgent.indexOf(<span class="string">"MSIE 8.0"</span>) &gt; <span class="number">0</span>) &#123;        </span><br><span class="line">    <span class="keyword">return</span> <span class="string">"MSIE8"</span>;</span><br><span class="line">&#125;<span class="keyword">else</span> <span class="keyword">if</span> (navigator.userAgent.indexOf(<span class="string">"MSIE 6.0"</span>) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"MSIE6"</span>;</span><br><span class="line">&#125;<span class="keyword">else</span> <span class="keyword">if</span> (navigator.userAgent.indexOf(<span class="string">"MSIE 7.0"</span>) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"MSIE7"</span>;</span><br><span class="line">&#125;<span class="keyword">else</span> <span class="keyword">if</span> (isFirefox = navigator.userAgent.indexOf(<span class="string">"Firefox"</span>) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"Firefox"</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">if</span> (navigator.userAgent.indexOf(<span class="string">"Chrome"</span>) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"Chrome"</span>;</span><br><span class="line">&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"Other"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>JS判断两个日期大小 </p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//得到日期值并转化成日期格式</span></span><br><span class="line"><span class="comment">//replace(/\-/g, "\/")是根据验证表达式把日期转化成长日期格式</span></span><br><span class="line"><span class="comment">//这样再进行判断就好判断了function ValidateDate() &#123;</span></span><br><span class="line">    <span class="keyword">var</span> beginDate = $(<span class="string">"#t_datestart"</span>).val();</span><br><span class="line">    <span class="keyword">var</span> endDate = $(<span class="string">"#t_dateend"</span>).val();</span><br><span class="line">    <span class="keyword">if</span> (beginDate.length &gt; <span class="number">0</span> &amp;&amp; endDate.length&gt;<span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> sDate = <span class="keyword">new</span> <span class="built_in">Date</span>(beginDate.replace(<span class="regexp">/\-/g</span>, <span class="string">"\/"</span>));                </span><br><span class="line">        <span class="keyword">var</span> eDate= <span class="keyword">new</span> <span class="built_in">Date</span>(endDate.replace(<span class="regexp">/\-/g</span>, <span class="string">"\/"</span>));                </span><br><span class="line">        <span class="keyword">if</span> (sDate &gt; eDate) &#123;</span><br><span class="line">            alert(<span class="string">'开始日期要小于结束日期'</span>); </span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>移除事件</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">this</span>.moveBind = <span class="function"><span class="keyword">function</span> (<span class="params">objId, eventType, callBack</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> obj = <span class="built_in">document</span>.getElementById(objId);</span><br><span class="line">    <span class="keyword">if</span> (obj.removeEventListener) &#123;</span><br><span class="line">        obj.removeEventListener(eventType, callBack, <span class="literal">false</span>);</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">window</span>.detachEvent) &#123;</span><br><span class="line">        obj.detachEvent(<span class="string">'on'</span> + eventType, callBack);</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        obj[<span class="string">'on'</span> + eventType] = <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>回车提交</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="string">"id"</span>).onkeypress = <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>&#123;    </span><br><span class="line">    event = (event) ? event : ((<span class="built_in">window</span>.event) ? <span class="built_in">window</span>.event : <span class="string">""</span>)</span><br><span class="line">    keyCode = event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode);    </span><br><span class="line">    <span class="keyword">if</span> (keyCode == <span class="number">13</span>) &#123;</span><br><span class="line">        $(<span class="string">"SubmitLogin"</span>).onclick();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>JS 执行计时器</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">timeStart = <span class="keyword">new</span> <span class="built_in">Date</span>().getTime();</span><br><span class="line">timesEnd = <span class="keyword">new</span> <span class="built_in">Date</span>().getTime();</span><br><span class="line"><span class="built_in">document</span>.getElementById(<span class="string">"time"</span>).innerHTML = timesEnd - timeStart;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>JS 写Cookie</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">setCookie</span>(<span class="params">name, value, expires, path, domain</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!expires) </span><br><span class="line">    expires = <span class="number">-1</span>;</span><br><span class="line">    <span class="keyword">if</span> (!path) </span><br><span class="line">    path = <span class="string">"/"</span>;</span><br><span class="line">    <span class="keyword">var</span> d = <span class="string">""</span> + name + <span class="string">"="</span> + value;</span><br><span class="line">    <span class="keyword">var</span> e;</span><br><span class="line">    <span class="keyword">if</span> (expires &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        e = <span class="string">""</span>;</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span> (expires == <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> f = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="number">1970</span>, <span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line">        e = <span class="string">";expires="</span> + f.toUTCString();</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">var</span> now = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line">        <span class="keyword">var</span> f = <span class="keyword">new</span> <span class="built_in">Date</span>(now.getTime() + expires * <span class="number">1000</span>);</span><br><span class="line">        e = <span class="string">";expires="</span> + f.toUTCString();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> dm;</span><br><span class="line">    <span class="keyword">if</span> (!domain) &#123;</span><br><span class="line">        dm = <span class="string">""</span>;</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        dm = <span class="string">";domain="</span> + domain;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">document</span>.cookie = name + <span class="string">"="</span> + value + <span class="string">";path="</span> + path + e + dm;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>JS 读Cookie</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">readCookie</span>(<span class="params">name</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> nameEQ = name + <span class="string">"="</span>;</span><br><span class="line">    <span class="keyword">var</span> ca = <span class="built_in">document</span>.cookie.split(<span class="string">';'</span>);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; ca.length; i++) &#123;</span><br><span class="line">        <span class="keyword">var</span> c = ca[i];</span><br><span class="line">        <span class="keyword">while</span> (c.charAt(<span class="number">0</span>) == <span class="string">' '</span>) c = c.substring(<span class="number">1</span>, c.length);</span><br><span class="line">        <span class="keyword">if</span> (c.indexOf(nameEQ) == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">decodeURIComponent</span>(c.substring(nameEQ.length, c.length))</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; </span><br><span class="line">    <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>Ajax 请求</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">C.ajax = <span class="function"><span class="keyword">function</span> (<span class="params">args</span>) </span>&#123;    <span class="keyword">var</span> self = <span class="keyword">this</span>;</span><br><span class="line">    <span class="keyword">this</span>.options = &#123;</span><br><span class="line">        type: <span class="string">'GET'</span>,</span><br><span class="line">        <span class="keyword">async</span>: <span class="literal">true</span>,</span><br><span class="line">        contentType: <span class="string">'application/x-www-form-urlencoded'</span>,</span><br><span class="line">        url: <span class="string">'about:blank'</span>,</span><br><span class="line">        data: <span class="literal">null</span>,</span><br><span class="line">        success: &#123;&#125;,</span><br><span class="line">        error: &#123;&#125;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">this</span>.getXmlHttp = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> xmlHttp;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        xmlhttp = <span class="keyword">new</span> XMLHttpRequest();</span><br><span class="line">    &#125;<span class="keyword">catch</span> (e) &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            xmlhttp = <span class="keyword">new</span> ActiveXObject(<span class="string">"Msxml2.XMLHTTP"</span>);</span><br><span class="line">        &#125;<span class="keyword">catch</span> (e) &#123;</span><br><span class="line">            xmlHttp = <span class="keyword">new</span> ActiveXObject(<span class="string">"Microsoft.XMLHTTP"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;<span class="keyword">if</span> (!xmlhttp) &#123;</span><br><span class="line">        alert(<span class="string">'您的浏览器不支持AJAX'</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">        <span class="keyword">return</span> xmlhttp;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">this</span>.send = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    C.each(self.options, <span class="function"><span class="keyword">function</span> (<span class="params">key, val</span>) </span>&#123;</span><br><span class="line">    self.options[key] = (args[key] == <span class="literal">null</span>) ? val : args[key];</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">var</span> xmlHttp = <span class="keyword">new</span> self.getXmlHttp();</span><br><span class="line">    <span class="keyword">if</span> (self.options.type.toUpperCase() == <span class="string">'GET'</span>) &#123;</span><br><span class="line">        xmlHttp.open(self.options.type, self.options.url + (self.options.data == <span class="literal">null</span> ? <span class="string">""</span> : ((<span class="regexp">/[?]$/</span>.test(self.options.url) ? <span class="string">'&amp;'</span> : <span class="string">'?'</span>) + self.options.data)), self.options.async);</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        xmlHttp.open(self.options.type, self.options.url, self.options.async);</span><br><span class="line">        xmlHttp.setRequestHeader(<span class="string">'Content-Length'</span>, self.options.data.length);</span><br><span class="line">    &#125;</span><br><span class="line">        xmlHttp.setRequestHeader(<span class="string">'Content-Type'</span>, self.options.contentType);</span><br><span class="line">        xmlHttp.onreadystatechange = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (xmlHttp.readyState == <span class="number">4</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (xmlHttp.status == <span class="number">200</span> || xmlHttp.status == <span class="number">0</span>) &#123;                    <span class="keyword">if</span> (<span class="keyword">typeof</span> self.options.success == <span class="string">'function'</span>) self.options.success(xmlHttp.responseText);</span><br><span class="line">            xmlHttp = <span class="literal">null</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">typeof</span> self.options.error == <span class="string">'function'</span>) </span><br><span class="line">        self.options.error(<span class="string">'Server Status: '</span> + xmlHttp.status);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        xmlHttp.send(self.options.type.toUpperCase() == <span class="string">'POST'</span> ? self.options.data.toString() : <span class="literal">null</span>);</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">this</span>.send();</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>JS StringBuilder 用法</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">StringBuilder</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"><span class="keyword">this</span>.strings = <span class="keyword">new</span> <span class="built_in">Array</span>;</span><br><span class="line">&#125;;</span><br><span class="line">StringBuilder.prototype.append = <span class="function"><span class="keyword">function</span> (<span class="params">str</span>) </span>&#123;</span><br><span class="line"><span class="keyword">this</span>.strings.push(str);</span><br><span class="line">&#125;;</span><br><span class="line">StringBuilder.prototype.toString = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line"><span class="keyword">return</span> <span class="keyword">this</span>.strings.join(<span class="string">''</span>);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>JS 加载到顶部LoadJS</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">loadJS</span> (<span class="params">url, fn</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> ss = <span class="built_in">document</span>.getElementsByName(<span class="string">'script'</span>),</span><br><span class="line">    loaded = <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, len = ss.length; i &lt; len; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (ss[i].src &amp;&amp; ss[i].getAttribute(<span class="string">'src'</span>) == url) &#123;</span><br><span class="line">            loaded = <span class="literal">true</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (loaded) &#123;</span><br><span class="line">        <span class="keyword">if</span> (fn &amp;&amp; <span class="keyword">typeof</span> fn != <span class="string">'undefined'</span> &amp;&amp; fn <span class="keyword">instanceof</span> <span class="built_in">Function</span>) </span><br><span class="line">        fn();            </span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> s = <span class="built_in">document</span>.createElement(<span class="string">'script'</span>),</span><br><span class="line">    b = <span class="literal">false</span>;</span><br><span class="line">    s.setAttribute(<span class="string">'type'</span>, <span class="string">'text/javascript'</span>);</span><br><span class="line">    s.setAttribute(<span class="string">'src'</span>, url);</span><br><span class="line">    s.onload = s.onreadystatechange = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!b &amp;&amp; (!<span class="keyword">this</span>.readyState || <span class="keyword">this</span>.readyState == <span class="string">'loaded'</span> || <span class="keyword">this</span>.readyState == <span class="string">'complete'</span>)) &#123;</span><br><span class="line">        b = <span class="literal">true</span>; </span><br><span class="line">        <span class="keyword">if</span> (fn &amp;&amp; <span class="keyword">typeof</span> fn != <span class="string">'undefined'</span> &amp;&amp; fn <span class="keyword">instanceof</span> <span class="built_in">Function</span>) </span><br><span class="line">        fn();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="built_in">document</span>.getElementsByTagName(<span class="string">'head'</span>)[<span class="number">0</span>].appendChild(s);</span><br><span class="line">    &#125;,</span><br><span class="line">bind: </span><br><span class="line"><span class="function"><span class="keyword">function</span> (<span class="params">objId, eventType, callBack</span>) </span>&#123;  <span class="comment">//适用于任何浏览器的绑定</span></span><br><span class="line">        <span class="keyword">var</span> obj = <span class="built_in">document</span>.getElementById(objId);        </span><br><span class="line">        <span class="keyword">if</span> (obj.addEventListener) &#123;</span><br><span class="line">            obj.addEventListener(eventType, callBack, <span class="literal">false</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">window</span>.attachEvent) &#123;</span><br><span class="line">            obj.attachEvent(<span class="string">'on'</span> + eventType, callBack);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            obj[<span class="string">'on'</span> + eventType] = callBack;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">JSLoad</span> (<span class="params">args</span>) </span>&#123; </span><br><span class="line">        s = <span class="built_in">document</span>.createElement(<span class="string">"script"</span>);</span><br><span class="line">        s.setAttribute(<span class="string">"type"</span>, <span class="string">"text/javascript"</span>);</span><br><span class="line">        s.setAttribute(<span class="string">"src"</span>, args.url);</span><br><span class="line">        s.onload = s.onreadystatechange = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!s.readyState || s.readyState == <span class="string">"loaded"</span> || s.readyState == <span class="string">"complete"</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">typeof</span> args.callback == <span class="string">"function"</span>) </span><br><span class="line">            args.callback(<span class="keyword">this</span>, args);</span><br><span class="line">            s.onload = s.onreadystatechange = <span class="literal">null</span>;                </span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                s.parentNode &amp;&amp; s.parentNode.removeChild(s);</span><br><span class="line">                &#125; <span class="keyword">catch</span> (e) &#123; &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;        </span><br><span class="line">        <span class="built_in">document</span>.getElementsByTagName(<span class="string">"head"</span>)[<span class="number">0</span>].appendChild(s);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>清空 LoadJS 加载到顶部的js引用</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">ClearHeadJs</span>  (<span class="params">src</span>) </span>&#123; </span><br><span class="line">        <span class="keyword">var</span> js = <span class="built_in">document</span>.getElementsByTagName(<span class="string">'head'</span>)[<span class="number">0</span>].children;</span><br><span class="line">        <span class="keyword">var</span> obj = <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; js.length; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (js[i].tagName.toLowerCase() == <span class="string">"script"</span> &amp;&amp; js[i].attributes[<span class="string">'src'</span>].value.indexOf(src) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            obj = js[i];</span><br><span class="line">           &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">document</span>.getElementsByTagName(<span class="string">'head'</span>)[<span class="number">0</span>].removeChild(obj);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>JS 替换非法字符主要用在密码验证上出现的特殊字符</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">URLencode</span>(<span class="params">sStr</span>) </span>&#123;</span><br><span class="line"><span class="keyword">return</span> <span class="built_in">escape</span>(sStr).replace(<span class="regexp">/\+/g</span>, <span class="string">'%2B'</span>).replace(<span class="regexp">/\"/g</span>, <span class="string">'%22'</span>).replace(<span class="regexp">/\'/g</span>, <span class="string">'%27'</span>).replace(<span class="regexp">/\//g</span>, <span class="string">'%2F'</span>);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>按Ctrl + Entert 直接提交表单</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.body.onkeydown = <span class="function"><span class="keyword">function</span> (<span class="params">evt</span>) </span>&#123;</span><br><span class="line">        evt = evt ? evt : (<span class="built_in">window</span>.event ? <span class="built_in">window</span>.event : <span class="literal">null</span>);</span><br><span class="line">        <span class="keyword">if</span> (<span class="number">13</span> == evt.keyCode &amp;&amp; evt.ctrlKey) &#123;</span><br><span class="line">            evt.returnValue = <span class="literal">false</span>;</span><br><span class="line">            evt.cancel = <span class="literal">true</span>;</span><br><span class="line">            PostData();</span><br><span class="line">        &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>获取当前时间</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">GetCurrentDate</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">var</span> d = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line">        <span class="keyword">var</span> y = d.getYear()+<span class="number">1900</span>;</span><br><span class="line">        month = add_zero(d.getMonth() + <span class="number">1</span>),</span><br><span class="line">        days = add_zero(d.getDate()),</span><br><span class="line">        hours = add_zero(d.getHours());</span><br><span class="line">        minutes = add_zero(d.getMinutes()),</span><br><span class="line">        seconds = add_zero(d.getSeconds());</span><br><span class="line">        <span class="keyword">var</span> str = y + <span class="string">'-'</span> + month + <span class="string">'-'</span> + days + <span class="string">' '</span> + hours + <span class="string">':'</span> + minutes + <span class="string">':'</span> + seconds;        </span><br><span class="line">        <span class="keyword">return</span> str;</span><br><span class="line">    &#125;; </span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add_zero</span>(<span class="params">temp</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (temp &lt; <span class="number">10</span>) <span class="keyword">return</span> <span class="string">"0"</span> + temp;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">return</span> temp;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>Js 去掉空格方法</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">String</span>.prototype.Trim = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; </span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.replace(<span class="regexp">/(^\s*)|(\s*$)/g</span>, <span class="string">""</span>); </span><br><span class="line">    &#125;</span><br><span class="line"><span class="built_in">String</span>.prototype.LTrim = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.replace(<span class="regexp">/(^\s*)/g</span>, <span class="string">""</span>);</span><br><span class="line">    &#125;</span><br><span class="line"><span class="built_in">String</span>.prototype.RTrim = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.replace(<span class="regexp">/(\s*$)/g</span>, <span class="string">""</span>);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>js 动态移除 head 里的 js 引用</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">this</span>.ClearHeadJs = <span class="function"><span class="keyword">function</span> (<span class="params">src</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> js = <span class="built_in">document</span>.getElementsByTagName(<span class="string">'head'</span>)[<span class="number">0</span>].children;</span><br><span class="line">    <span class="keyword">var</span> obj = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; js.length; i++) &#123;</span><br><span class="line">    <span class="keyword">if</span> (js[i].tagName.toLowerCase() == <span class="string">"script"</span> &amp;&amp; js[i].attributes[<span class="string">'src'</span>].value.indexOf(src) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        obj = js[i];</span><br><span class="line">    &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">document</span>.getElementsByTagName(<span class="string">'head'</span>)[<span class="number">0</span>].removeChild(obj);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>整个URL 点击事件 加在URL里的onclick里</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">CreateFrom</span>(<span class="params">url, params</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> f = <span class="built_in">document</span>.createElement(<span class="string">"form"</span>);</span><br><span class="line">    f.setAttribute(<span class="string">"action"</span>, url);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; params.length; i++) &#123;</span><br><span class="line">         <span class="keyword">var</span> input = <span class="built_in">document</span>.createElement(<span class="string">"input"</span>);</span><br><span class="line">         input.setAttribute(<span class="string">"type"</span>, <span class="string">"hidden"</span>);</span><br><span class="line">         input.setAttribute(<span class="string">"name"</span>, params[i].paramName);</span><br><span class="line">         input.setAttribute(<span class="string">"value"</span>, params[i].paramValue);</span><br><span class="line">         f.appendChild(input);</span><br><span class="line">    &#125;</span><br><span class="line">    f.target = <span class="string">"_blank"</span>;</span><br><span class="line">    <span class="built_in">document</span>.body.appendChild(f);            </span><br><span class="line">    f.submit();</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>判断浏览器使用的是哪个 JS 版本</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;script language=<span class="string">"javascript"</span>&gt;</span><br><span class="line">      <span class="keyword">var</span> jsversion = <span class="number">1.0</span>;    &lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">    &lt;script language="javascript1.1"&gt;</span></span><br><span class="line"><span class="regexp">      jsversion = 1.1;    &lt;/</span>script&gt;</span><br><span class="line">    &lt;script language=<span class="string">"javascript1.2"</span>&gt;</span><br><span class="line">      jsversion = <span class="number">1.2</span>;    &lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">    ......</span></span><br><span class="line"><span class="regexp">    ......</span></span><br><span class="line"><span class="regexp">    &lt;script language="javascript1.9"&gt;</span></span><br><span class="line"><span class="regexp">      jsversion = 1.9;    &lt;/</span>script&gt;</span><br><span class="line">    &lt;script language=<span class="string">"javascript2.0"</span>&gt;</span><br><span class="line">      jsversion = <span class="number">2.0</span>;    &lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">    alert(jsversion);</span></span><br></pre></td></tr></table></figure>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-08-29T07:51:17.819Z" itemprop="dateUpdated">2017-08-29 15:51:17</time>
</span><br>


        
        原文链接：<a href="/2017/08/29/JavaScript常用代码集合/" target="_blank" rel="external">https://lvshen9.gitee.io/2017/08/29/JavaScript常用代码集合/</a>
        
    </div>
    
    <footer>
        <a href="https://lvshen9.gitee.io">
            <img src="/img/avatar.jpg" alt="我的技术小房间">
            我的技术小房间
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JavaScript/">JavaScript</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/前端/">前端</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lvshen9.gitee.io/2017/08/29/JavaScript常用代码集合/&title=《JavaScript常用代码集合》 — Lvshen's Blog&pic=https://lvshen9.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lvshen9.gitee.io/2017/08/29/JavaScript常用代码集合/&title=《JavaScript常用代码集合》 — Lvshen's Blog&source=
手机类型判断

1234567var BrowserInfo = &#123;        userAgent: navigator.userAgen..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lvshen9.gitee.io/2017/08/29/JavaScript常用代码集合/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《JavaScript常用代码集合》 — Lvshen's Blog&url=https://lvshen9.gitee.io/2017/08/29/JavaScript常用代码集合/&via=https://lvshen9.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lvshen9.gitee.io/2017/08/29/JavaScript常用代码集合/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/08/30/我在Github上一个关于俄罗斯方块的项目/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">我在Github上一个关于俄罗斯方块的项目</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/08/29/如何在MySQL中使用索引/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">如何在MySQL中使用索引</h4>
      </a>
    </div>
  
</nav>



    











    <!-- Valine Comments -->
    <div class="comments vcomment" id="comments"></div>
    <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
    <script src="//unpkg.com/valine@latest/dist/Valine.min.js"></script>
    <!-- Valine Comments script -->
    <script>
        var GUEST_INFO = ['nick','mail','link'];
        var guest_info = 'nick,mail,link'.split(',').filter(function(item){
          return GUEST_INFO.indexOf(item) > -1
        });
        new Valine({
            el: '#comments',
            notify: 'false' == 'true',
            verify: 'false' == 'true',
            appId: "dy9kXHwg5jQUlLryQmpjWRlM-gzGzoHsz",
            appKey: "P9Nh39Ol0JbMMiYqNGHEP3ml",
            avatar: "mm",
            placeholder: "Just go go",
            guest_info: guest_info.length == 0 ? GUEST_INFO : guest_info,
            pageSize: "10"
        })
    </script>
    <!-- Valine Comments end -->







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="https://lvshen9.github.io/blog2/pay/weixin.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="https://lvshen9.github.io/blog2/pay/weixin.jpg" data-alipay="https://lvshen9.github.io/blog2/pay/zhifu.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>我的技术小房间 &copy; 2015 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lvshen9.gitee.io/2017/08/29/JavaScript常用代码集合/&title=《JavaScript常用代码集合》 — Lvshen's Blog&pic=https://lvshen9.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lvshen9.gitee.io/2017/08/29/JavaScript常用代码集合/&title=《JavaScript常用代码集合》 — Lvshen's Blog&source=
手机类型判断

1234567var BrowserInfo = &#123;        userAgent: navigator.userAgen..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lvshen9.gitee.io/2017/08/29/JavaScript常用代码集合/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《JavaScript常用代码集合》 — Lvshen's Blog&url=https://lvshen9.gitee.io/2017/08/29/JavaScript常用代码集合/&via=https://lvshen9.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lvshen9.gitee.io/2017/08/29/JavaScript常用代码集合/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=https://lvshen9.gitee.io/2017/08/29/JavaScript常用代码集合/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
